<template>
	<div class="divwrapper">
		<el-breadcrumb separator="/" class="bread-title">
		   <el-breadcrumb-item >配置中心</el-breadcrumb-item>
		   <el-breadcrumb-item >金蝶对接</el-breadcrumb-item>
		   <el-breadcrumb-item>楼盘映射组织关系</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form :inline="true" :model="ruleForm" class="demo-form-inline">
			<el-form-item label="城市：">
			    <el-select v-model="ruleForm.cityId" filterable >
			        <el-option v-for="item in citys"
			                   :key="item.cityId"
			                   :label="item.city"
			                   :value="item.cityId">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="楼盘名称：">
			  	 <el-input v-model.trim="ruleForm.devName"></el-input>
			</el-form-item>
			<el-form-item label="执行状态：">
				<el-select v-model="ruleForm.devStatus">
					<el-option v-for="item in stateList" :label="item.label" :value="item.id" :key="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary"  @click="searchList" id="monitorEnter">查询</el-button>
				<el-button  @click="resethander">重置</el-button>

			</el-form-item>
		</el-form>

		<el-table :data="tableData" border style="width: 100%">
			<el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
			<el-table-column prop="city" label="城市" align="center"></el-table-column>
			<el-table-column prop="devName" label="楼盘名称" align="center"></el-table-column>
			<el-table-column prop="devId" label="楼盘id" align="center"></el-table-column>
			<el-table-column label="执行状态" :formatter="initStatus" align="center"></el-table-column>
			<!-- 新增2.1 -->
			<el-table-column prop="beginEndTime" label="合作日期（起-止）" align="center" width="150"></el-table-column>
			<el-table-column prop="orgCode" label="组织编码" align="center"></el-table-column>
			<el-table-column prop="orgName" label="组织名称" align="center"></el-table-column>
			<el-table-column prop="userName" label="操作人" align="center"></el-table-column>
			<el-table-column label="操作时间" align="center">
				<template slot-scope="scope">
					<TimeComp :timestamp="scope.row.updateTime"></TimeComp>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" width="180">
			  <template slot-scope='scope'>
                <el-button type="text" @click="btnhander('add',scope.row)">新增版本</el-button>
			    <el-button type="text" @click="btnhander('history',scope.row)">版本管理</el-button>
			  </template>
			</el-table-column>
		</el-table>
		<div class="pagebar">
		  <el-pagination
		    background
		    layout="total, sizes, prev, pager, next"
		    :current-page.sync="ruleForm.pageNum"
		    :page-sizes="[10, 20, 30, 40]"
		    :page-size="ruleForm.pageSize"
		    @size-change="handleSizeChange"
		    @current-change="handleCurrentChange"
		    :total="totalSize">
		  </el-pagination>
		</div>
		<!-- 编辑弹框 -->
		<el-dialog
		  title="新增版本"
		  :visible.sync="dialogVisible"
		  width="600px"
		  center>
		   <el-form  ref="ruleForm" label-width="140px" class="demo-ruleForm">
			  	<el-form-item label="城市:">
			  		<span>{{currentItem.city}}</span>
			  	</el-form-item>
			  	<el-form-item label="楼盘名称:">
			  		<span>{{currentItem.devName}}</span>
			  	</el-form-item>
			  	<el-form-item label="楼盘id:">
			  		<span>{{currentItem.devId}}</span>
			  	</el-form-item>

				<el-form-item label="合作时间（开始）:" required>
				    <el-date-picker
				      style="width:300px"
				      v-model="beginTime"
				      type="date"
				      value-format="timestamp"
				      :picker-options="pickerOptions0"
				      placeholder="选择日期时间">
				    </el-date-picker>
				</el-form-item>
				<el-form-item label="合作时间（结束）:" required>
					<el-date-picker
					  style="width:300px"
					  v-model="endTime"
					  value-format="timestamp"
					  type="date"
					  :picker-options="pickerOptions1"
					  placeholder="选择日期时间">
					</el-date-picker>
				</el-form-item>
			  	<el-form-item label="组织名称:" required>
			  		<el-select v-model="relevancyCode" filterable style="width:300px">
			  		    <el-option v-for="item in relevancy"
			  		               :key="item.id"
			  		               :label="item.orgName"
			  		               :value="item.orgCode">
			  		    </el-option>
			  		</el-select>
			  	</el-form-item>
			  	<el-form-item label="组织编码:" required>
			  		<span>{{relevancyCode}}</span>
			  	</el-form-item>
			  	<div style="text-align: center;">
			  		<el-button type="primary"  @click="ensurehander">确定</el-button>
			  		<el-button  @click="cancelhander">取消</el-button>
			  	</div>
			</el-form>
		</el-dialog>
	</div>
</template>
<script>
	import TimeComp from '../common/timeComp';
	import api from '@/api/apiPath';
	import utils from "@/common/utils.js";

	export default {
		name:'buildMembership',
		title:'楼盘映射组织关系',
		components: {TimeComp},
		data(){
			return {
				citys:[],
				houseList: [],
				tableData:[],
				relevancy:[],
				relevancyCode:'',
				stateList: [
					{id: undefined, label: '全部'},
					{id: 1, label: '执行中'},
					{id: 0, label: '未执行'},
					{id: 2, label: '执行结束'},
				],
				ruleForm: {
					devStatus:undefined,
					cityId:undefined,
					devName:undefined,
					pageNum: 0,
					pageSize: 10,
				},
				currentItem:{},
				totalSize:0,
				dialogVisible: false,
				beginTime:'',
				endTime:'',
                pickerOptions0: {
                    disabledDate: (time) => {
                        if (this.endTime) {
                            return this.endTime < time.getTime()+8.64e7;
                        }
                    }
                },
                pickerOptions1: {
                    disabledDate: (time) => {
                    	if (this.beginTime) {
                    		return this.beginTime > time.getTime()-8.64e7;
                    	}
                    }
                },
			}
		},
		methods:{
			resethander(){
				this.ruleForm.devStatus= '';
				this.ruleForm.cityId ='';
				this.ruleForm.devName = '';
			},
			initStatus(row){
				if (row.devStatus == 1) {
					return '执行中'
				} else if (row.devStatus == 0) {
					return '未执行'
				} else if (row.devStatus == 2) {
					return '执行结束'
				}
			},
			handleSizeChange(val) {
			    this.ruleForm.pageSize = val;
			    this.loadData();
			},
			handleCurrentChange(val) {
			    this.ruleForm.pageNum = val;
			    this.loadData();
			},
			btnhander(type,row){
				if (type == 'del') {
					this.$confirm(`确定删除"${row.devName}"组织映射关系？`, '提示', {
						 confirmButtonText: '确定',
						 cancelButtonText: '取消',
						 type: 'warning'
						}).then(() => {
							this.axios.post(api.KINGDEERELEVANCYDEL,{devId:row.devId}).then(res => {
								if (res.data.code !== 200) {
									this.$message(res.data.msg)
								}else{
									this.loadData();
								}
							})
						}).catch(() => {
						 this.$message({
						   type: 'info',

						   message: '已取消删除'
						 });
					});
				}else if(type == 'add'){
					this.beginTime = '';
					this.endTime = '';
					this.relevancyCode = '';
					this.currentItem = row;
					// this.relevancyCode = this.currentItem.orgCode;//组织编码
					this.selectAllOrg();//选择组织名称
					this.dialogVisible = true;
				}else if(type == 'history'){
					this.$router.push({path:'/buildingHistory',query:{devId:row.devId}})
				}
			},
			selectAllOrg(){
				let obj = {}
				obj.devId = this.currentItem.devId
				obj.orgCode = this.currentItem.orgCode
				obj.orgName = this.currentItem.orgName
				this.axios.post(api.KINGDEESELECTALLORG,obj).then(res => {
					if (res.data.code !== 200) {
						this.$message(res.data.msg)
					}else{
						this.relevancy = res.data.data
					}
				})
			},
			ensurehander(){
				if (this.beginTime == '') {
					this.$message({
						message:'请输入合作时间(开始)',
						type:'warning'
					})
					return
				}
				if (this.endTime == '') {
					this.$message({
						message:'请输入合作时间(结束)',
						type:'warning'
					})
					return
				}
				if (this.relevancyCode == '' || !this.relevancyCode) {
					this.$message({
						message:'请选择组织名称',
						type:'warning'
					})
					return
				}

				let ritem= this.relevancy.find(val=>{
					return val.orgCode == this.relevancyCode
				})
				let obj = {}
				obj.beginTime =this.beginTime;
				obj.cityName = this.currentItem.city;
				obj.devId = this.currentItem.devId;
				obj.devName = this.currentItem.devName
				obj.endTime = this.endTime;
				obj.orgCode = ritem.orgCode;
				obj.orgName = ritem.orgName;
				this.$axios.post('/kingdee/relevancy/addVersion',obj).then(res=>{
					this.$message({
						message:'新增成功',
						type:'success'
					})
					this.dialogVisible = false
					this.loadData();
				})
			},
			cancelhander(){
				this.dialogVisible = false
			},
			loadData(){
				this.axios.post(api.KINGDEERELEVANCYLIST,this.ruleForm).then(res => {
					if (res.data.code !== 200) {
						this.$message(res.data.msg)
					}else{
						this.tableData = res.data.data.datas;
						this.totalSize = res.data.data.total;
					}
				})
			},
			loadCity(){
				this.axios.post(api.CITYLIST).then(res => {
					if (res.data.code !== 200) {
						this.$message(res.data.msg)
					}else {
						this.citys = res.data.data.cityList
					}
				})
			},
			searchList(){
				this.ruleForm.pageNum =1;
				this.loadData();
			}
		},
		activated(){
			this.searchList();
		},
		created(){
			 this.loadCity();
			 this.loadData();
		}
	}
</script>
<style lang="stylus" scoped>
	// .flexstyle {
	// 	display: table
	// 	margin: 0 auto
	// }
</style>
